<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>

<head>
<c:if test="${fn:contains(sessionInfo.resourceList, '/chargeChemicalExam/edit')}">
	<script type="text/javascript">
		$.canEdit = true;
	</script>
</c:if>
<script>
	var registerCharges = [];
	var registerId = "${registerId}";
	
	$('#chemicalExam').combobox({    
	    url:'${ctx}/chemicalExam/list',    
	    valueField:'id',    
	    textField:'chemicalExamItem'   
	}); 
	
</script>

<c:forEach items="${registerCharges}" var="registerCharge">
	<script type="text/javascript">
		var registerCharge = {
			'id':"${registerCharge.id}",
			'chemicalExamItem': "${registerCharge.chemicalExamItem}",
			'addTime': "${registerCharge.addTime}",
			'chargeTime':"${registerCharge.chargeTime}",
			'chemicalExamAddTime':"${registerCharge.chemicalExamAddTime}",
			'chemicalExamResult':"${registerCharge.chemicalExamResult}"
		};

		registerCharges.push(registerCharge);
	</script>
</c:forEach>

<script type="text/javascript">
	
	$(function() {
		dataGrid = $('#dataGrid').datagrid({
			striped : true,
			rownumbers : true,
			singleSelect : true,
			idField : 'id',
			sortName : 'id',
			sortOrder : 'asc',
			columns :[[{
				width : '100',
				title : '化验项目',
				field : 'chemicalExamItem'
			},{
				width : '100',
				title : 'id',
				field : 'id',
				hidden: true
			}, {
				width : '150',
				title : '开单时间',
				field : 'addTime',
			},{
				width : '150',
				title : '缴费时间',
				field : 'chargeTime'
			}, {
				width : '150',
				title : '化验时间',
				field : 'chemicalExamAddTime'
			},{
				width : '150',
				title : '化验结果',
				field : 'chemicalExamResult'
			},{
				field : 'action',
				title : '操作',
				width : 200,
				formatter : function(value, row, index) {
					var str = '&nbsp;&nbsp;';
					if(row.chargeTime == '' && $.canEdit) {
						str += '<a href="javascript:void(0)" onclick="charge(\'{0}\');" >缴费</a>';
						str += '&nbsp;&nbsp;';
					}
					if(row.chargeTime != '' && row.chemicalExamAddTime == '') {
						str  += "<a href=\"javascript:void(0)\" onclick=\"addChemicalExam(\'{1}\',\'{2}\');\" >化验</a>";
					}
					str = $.formatString(str, row.id,row.id,row.chemicalExamResult);
					return str;
				}
			} ] ],
			data: registerCharges,
			toolbar : '#toolbar'
		});
	});
	
	function addCharge() {
		var chemicalExamId = $('#chemicalExam').combobox('getValue');
		if(chemicalExamId == '') {
			$.messager.alert('错误', '未选择化验项目', 'error');
		} else {
			var registerId = "${registerId}";
			
			$.ajax({
				type: "POST",
				async: false,
				data:  {
					'chemicalExamId':chemicalExamId,
					'registerId':registerId
				},
		        url: '${ctx}/chargeChemicalExam/addCharge',
		        dataType: "json",
		        success:function(result) {
		        	if(result.status.errorCode == 0) {
		        		$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
		        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/chargeChemicalExam/detailPage?registerId=' + registerId);
		        		});
		        	} else {
		        		$.messager.alert('错误', result.status.errorMsg, 'error');
		        	}
		        },
		        error:function(result) {
		        }
	   	 	});
		}
	}
	
	function charge(registerChargeId) {
		$.ajax({
			type: "POST",
			async: false,
			data:  {
				'registerChargeId':registerChargeId
			},
	        url: '${ctx}/chargeChemicalExam/charge',
	        dataType: "json",
	        success:function(result) {
	        	if(result.status.errorCode == 0) {
	        		$.messager.alert('成功', result.status.errorMsg, 'info',function(r){
	        			parent.$.modalDialog.handler.dialog('refresh','${ctx}/chargeChemicalExam/detailPage?registerId=' + registerId);
	        		});
	        	} else {
	        		$.messager.alert('错误', result.status.errorMsg, 'error');
	        	}
	        },
	        error:function(result) {
	        }
   	 	});
	}
	
	function addChemicalExam(registerChargeId,chemicalExamResult) {
		debugger;
		var dialogHandler;
		parent.$.dialogHandler = dialogHandler = $('<div/>').dialog({
			title:'化验结果',
			width:700,
			height:400,
			modal:true,
			href:'${ctx}/chargeChemicalExam/addChemicalExamPage?registerChargeId='+registerChargeId + "&chemicalExamResult=" + chemicalExamResult,
			onClose:function(){
				parent.$.modalDialog.handler.dialog('refresh','${ctx}/chargeChemicalExam/detailPage?registerId=' + registerId);
				parent.$.dialogHandler = undefined;
				dialogHandler.dialog('destroy');
			},
			buttons : [ {
				text : '添加',
				handler : function() {
					debugger;
	        		var f = dialogHandler.find("#chargeChemicalExamAddForm");
	        		f.submit();
				}
			} ]
		});
		
		
		
	}
</script>
</head>

<div class="easyui-layout" data-options="fit:true,border:false">
	
	<div data-options="region:'center',fit:true,border:false">
		<table id="dataGrid" data-options="fit:true,border:false"></table>
	</div>
	
	<div id="toolbar" style="display: none;">
			<a onclick="addCharge();" href="javascript:void(0);"
				class="easyui-linkbutton"
				data-options="plain:true,iconCls:'icon_add'">开单新化验</a>
			
			<select id="chemicalExam" style="width: 140px; height: 29px;"></select>
			
	</div>
</div>
